#include <..\\standard.hlsli>
#include <..\\WR2Parameters.hlsli>

VertexToPixel vs_main(VS_INPUT Input)
{
	VertexToPixel Output = (VertexToPixel)0;
	Output.pos = float4(mul(Input.Position, worldViewProj));
	Output.normal = mul(Input.Normal, (float3x3)world);

	Output.kColor = float4(Input.kColor.rgb * 2, Input.kColor.a);
	//Output.kColor = float4(0.5,0.5,0.5,1); // red green blue shadow

	Output.kBlend = Input.kblend;

	Output.pos2 = Input.Position;

	Output.texCoord1 = float4(Input.Tex.x,
		Input.Tex.y,
		Input.Tex.x * uvTexture2a.x + Input.Tex.y * uvTexture2a.y + uvTexture2a.w,
		Input.Tex.x * uvTexture2b.x + Input.Tex.y * uvTexture2b.y + uvTexture2b.w);

	// Output.texCoord2 = float4(Input.Position.x * uvTexture1a.x + Input.Position.y * uvTexture1a.y + Input.Position.z * uvTexture1a.z + uvTexture1a.w,
	//                           Input.Position.x * uvTexture1b.x + Input.Position.y * uvTexture1b.y + Input.Position.z * uvTexture1b.z + uvTexture1b.w,
	//                           Input.Position.x * uvTexture2a.x + Input.Position.y * uvTexture2a.y + Input.Position.z * uvTexture2a.z + uvTexture2a.w,
	//                           Input.Position.x * uvTexture2b.x + Input.Position.y * uvTexture2b.y + Input.Position.z * uvTexture2b.z + uvTexture2b.w);
	//
	// Output.texCoord3.xy = float2(Input.Position.x * uvTexture3a.x + Input.Position.y * uvTexture3a.y + Input.Position.z * uvTexture3a.z + uvTexture3a.w,
	//                              Input.Position.x * uvTexture3b.x + Input.Position.y * uvTexture3b.y + Input.Position.z * uvTexture3b.z + uvTexture3b.w);

	float d = distance(Input.Position, 0);
	float fFogStart = 2000;
	float fFogEnd = 2500;
	Output.foger = saturate((fFogEnd - d) / (fFogEnd - fFogStart));
	return Output;
};

struct PixelToFrame
{
	float4 Color;
};

PixelToFrame ps_main(VertexToPixel PSIn) : SV_TARGET
{
	PixelToFrame Output = (PixelToFrame)0;

	float3 Ln = -lightDir;
		float3 Nn = normalize(PSIn.normal);
		float3 In = normalize(PSIn.pos2.xyz - CameraPosition);// View direction
		float3 Hn = normalize(Ln - In);         // Half way between view & light
		//float3 R = reflect(In,Nn);                            //Env map

		float4 Lay1 = xColoredTexture1_Tex.Sample(Tex1, PSIn.texCoord1.xy);
		float4 Lay2 = xColoredTexture2_Tex.Sample(Tex2, PSIn.texCoord1.zw);

		float c = smoothstep(0.4375, 0.5625, PSIn.kBlend.b);

	//float ShadowCol = smoothstep (0.4375, 0.5625, PSIn.kColor.a);

	float ShadowCol = smoothstep(0.4375, 0.5625, 1.0 - PSIn.kBlend.w);

	float Spec = pow(max(0., dot(Nn, Hn)), 32.)*ShadowCol*0.5;
	float Diff = max(0., dot(Nn, Ln))*1.8;

	float3 TexColor = (Lay1.rgb + Spec*Lay1.a)*(1. - c) + (Lay2.rgb + Spec*Lay2.a / 2.)*c;

		Output.Color = float4(TexColor*(SunColor*Diff*ShadowCol + PSIn.kColor.rgb), 1);

	return  Output;
}

technique Material144
{
	pass Material00
	{
		VertexShader = compile vs_1_1 vs_main();
		PixelShader = compile ps_2_0  ps_main();
	}
}